package com.volunteer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volunteer.entity.Activity;
import com.volunteer.entity.Registration;
import com.volunteer.entity.User;
import com.volunteer.mapper.ActivityMapper;
import com.volunteer.mapper.RegistrationMapper;
import com.volunteer.mapper.UserMapper;
import com.volunteer.service.StatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class StatServiceImpl implements StatService {

    private final ActivityMapper activityMapper;
    private final UserMapper userMapper;
    private final RegistrationMapper registrationMapper;

    @Override
    public Map<String, Object> getPlatformStats() {
        Map<String, Object> stats = new HashMap<>();

        // 统计活动总数
        long activityCount = activityMapper.selectCount(new LambdaQueryWrapper<>());
        stats.put("activityCount", activityCount);

        // 统计志愿者总数 (普通用户角色为1)
        long volunteerCount = userMapper.selectCount(new LambdaQueryWrapper<User>()
                .eq(User::getRole, 1));
        stats.put("volunteerCount", volunteerCount);

        // 统计累计服务时长 (所有已完成活动的时长总和)
        Long totalServiceHours = activityMapper.sumServiceHours();
        stats.put("totalServiceHours", totalServiceHours != null ? totalServiceHours : 0);

        return stats;
    }
    
    @Override
    public Map<String, Object> getAdminDashboardStats() {
        Map<String, Object> stats = new HashMap<>();
        
        // 总活动数
        long totalActivities = activityMapper.selectCount(new LambdaQueryWrapper<>());
        stats.put("totalActivities", totalActivities);
        
        // 总用户数
        long totalUsers = userMapper.selectCount(new LambdaQueryWrapper<>());
        stats.put("totalUsers", totalUsers);
        
        // 待审核活动数(status = 0)
        long pendingActivities = activityMapper.selectCount(new LambdaQueryWrapper<Activity>()
                .eq(Activity::getStatus, 0));
        stats.put("pendingActivities", pendingActivities);
        
        // 已完成活动数(status = 3)
        long completedActivities = activityMapper.selectCount(new LambdaQueryWrapper<Activity>()
                .eq(Activity::getStatus, 3));
        stats.put("completedActivities", completedActivities);
        
        // 最新活动列表(最近5个)
        List<Map<String, Object>> latestActivities = activityMapper.findLatestActivities(5);
        stats.put("latestActivities", latestActivities);
        
        // 最新用户列表(最近5个)
        List<Map<String, Object>> latestUsers = userMapper.findLatestUsers(5);
        stats.put("latestUsers", latestUsers);
        
        // 活动类型分布 (模拟数据，实际应该从数据库统计)
        List<Map<String, Object>> activityTypes = new ArrayList<>();
        activityTypes.add(createTypeMap("环保类", 35));
        activityTypes.add(createTypeMap("教育类", 25));
        activityTypes.add(createTypeMap("文化类", 20));
        activityTypes.add(createTypeMap("社区服务", 15));
        activityTypes.add(createTypeMap("其他", 5));
        stats.put("activityTypes", activityTypes);
        
        // 评价分布 (模拟数据，实际应该从数据库统计)
        List<Map<String, Object>> evaluationDistribution = new ArrayList<>();
        evaluationDistribution.add(createTypeMap("非常满意", 48));
        evaluationDistribution.add(createTypeMap("满意", 32));
        evaluationDistribution.add(createTypeMap("一般", 15));
        evaluationDistribution.add(createTypeMap("不满意", 5));
        stats.put("evaluationDistribution", evaluationDistribution);
        
        return stats;
    }
    
    private Map<String, Object> createTypeMap(String name, int value) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("value", value);
        return map;
    }
    
    @Override
    public Map<String, Object> getActivityTrend(String timeRange) {
        Map<String, Object> result = new HashMap<>();
        
        List<String> xAxis = new ArrayList<>();
        List<Integer> activityData = new ArrayList<>();
        List<Integer> registrationData = new ArrayList<>();
        
        // 根据时间范围生成数据
        switch (timeRange) {
            case "week":
                // 近7天数据
                generateWeekData(xAxis, activityData, registrationData);
                break;
            case "month":
                // 近30天数据
                generateMonthData(xAxis, activityData, registrationData);
                break;
            case "year":
                // 近12个月数据
                generateYearData(xAxis, activityData, registrationData);
                break;
            default:
                // 默认为月数据
                generateMonthData(xAxis, activityData, registrationData);
        }
        
        result.put("xAxis", xAxis);
        result.put("activityData", activityData);
        result.put("registrationData", registrationData);
        
        return result;
    }
    
    @Override
    public Map<String, Object> getUserGrowthTrend(String timeRange) {
        Map<String, Object> result = new HashMap<>();
        
        List<String> xAxis = new ArrayList<>();
        List<Integer> userData = new ArrayList<>();
        
        // 根据时间范围生成数据
        switch (timeRange) {
            case "week":
                // 近7天数据
                generateWeekUserData(xAxis, userData);
                break;
            case "month":
                // 近30天数据
                generateMonthUserData(xAxis, userData);
                break;
            case "year":
                // 近12个月数据
                generateYearUserData(xAxis, userData);
                break;
            default:
                // 默认为月数据
                generateMonthUserData(xAxis, userData);
        }
        
        result.put("xAxis", xAxis);
        result.put("userData", userData);
        
        return result;
    }
    
    // 生成近7天的活动数据
    private void generateWeekData(List<String> xAxis, List<Integer> activityData, List<Integer> registrationData) {
        String[] days = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        Integer[] activities = {3, 5, 8, 4, 6, 9, 7};
        Integer[] registrations = {25, 40, 65, 35, 48, 72, 56};
        
        xAxis.addAll(Arrays.asList(days));
        activityData.addAll(Arrays.asList(activities));
        registrationData.addAll(Arrays.asList(registrations));
    }
    
    // 生成近30天的活动数据
    private void generateMonthData(List<String> xAxis, List<Integer> activityData, List<Integer> registrationData) {
        String[] dates = {"9/1", "9/5", "9/10", "9/15", "9/20", "9/25", "9/30"};
        Integer[] activities = {5, 8, 12, 6, 9, 15, 10};
        Integer[] registrations = {42, 68, 96, 65, 85, 120, 98};
        
        xAxis.addAll(Arrays.asList(dates));
        activityData.addAll(Arrays.asList(activities));
        registrationData.addAll(Arrays.asList(registrations));
    }
    
    // 生成近12个月的活动数据
    private void generateYearData(List<String> xAxis, List<Integer> activityData, List<Integer> registrationData) {
        String[] months = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
        Integer[] activities = {15, 12, 20, 25, 28, 22, 18, 15, 30, 35, 25, 20};
        Integer[] registrations = {120, 95, 160, 200, 220, 175, 145, 120, 240, 280, 200, 160};
        
        xAxis.addAll(Arrays.asList(months));
        activityData.addAll(Arrays.asList(activities));
        registrationData.addAll(Arrays.asList(registrations));
    }
    
    // 生成近7天的用户数据
    private void generateWeekUserData(List<String> xAxis, List<Integer> userData) {
        String[] days = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        Integer[] users = {8, 10, 15, 12, 18, 20, 16};
        
        xAxis.addAll(Arrays.asList(days));
        userData.addAll(Arrays.asList(users));
    }
    
    // 生成近30天的用户数据
    private void generateMonthUserData(List<String> xAxis, List<Integer> userData) {
        String[] dates = {"9/1", "9/5", "9/10", "9/15", "9/20", "9/25", "9/30"};
        Integer[] users = {15, 12, 18, 24, 20, 28, 22};
        
        xAxis.addAll(Arrays.asList(dates));
        userData.addAll(Arrays.asList(users));
    }
    
    // 生成近12个月的用户数据
    private void generateYearUserData(List<String> xAxis, List<Integer> userData) {
        String[] months = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
        Integer[] users = {45, 32, 56, 68, 50, 42, 38, 35, 70, 85, 65, 55};
        
        xAxis.addAll(Arrays.asList(months));
        userData.addAll(Arrays.asList(users));
    }
}
